package com.ruoyi.duanju.controller;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.duanju.service.IPlaywrightService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.duanju.domain.DjDuanju;
import com.ruoyi.duanju.service.IDjDuanjuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 短剧列表Controller
 * 
 * @author fyliu
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/duanju/duanju")
public class DjDuanjuController extends BaseController
{
    @Autowired
    private IDjDuanjuService djDuanjuService;

    @Autowired
    private IPlaywrightService iPlaywrightService;

    /**
     * 查询短剧列表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DjDuanju djDuanju)
    {
        startPage();
        List<DjDuanju> list = djDuanjuService.selectDjDuanjuList(djDuanju);
        if(StringUtils.isNotEmpty(djDuanju.getTitle()) && CollectionUtils.isEmpty(list)){
            //查询第三方数据库
            list = iPlaywrightService.search(djDuanju.getTitle());
        }
        return getDataTable(list);
    }

    /**
     * 导出短剧列表列表
     */
    @PreAuthorize("@ss.hasPermi('duanju:duanju:export')")
    @Log(title = "短剧列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DjDuanju djDuanju)
    {
        List<DjDuanju> list = djDuanjuService.selectDjDuanjuList(djDuanju);
        ExcelUtil<DjDuanju> util = new ExcelUtil<DjDuanju>(DjDuanju.class);
        util.exportExcel(response, list, "短剧列表数据");
    }

    /**
     * 获取短剧列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('duanju:duanju:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(djDuanjuService.selectDjDuanjuById(id));
    }

    /**
     * 新增短剧列表
     */
    @PreAuthorize("@ss.hasPermi('duanju:duanju:add')")
    @Log(title = "短剧列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DjDuanju djDuanju)
    {
        return toAjax(djDuanjuService.insertDjDuanju(djDuanju));
    }

    /**
     * 修改短剧列表
     */
    @PreAuthorize("@ss.hasPermi('duanju:duanju:edit')")
    @Log(title = "短剧列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DjDuanju djDuanju)
    {
        return toAjax(djDuanjuService.updateDjDuanju(djDuanju));
    }

    /**
     * 删除短剧列表
     */
    @PreAuthorize("@ss.hasPermi('duanju:duanju:remove')")
    @Log(title = "短剧列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(djDuanjuService.deleteDjDuanjuByIds(ids));
    }
}
